<?php
class variableEditAction extends sfActions
{
	public function execute($request)
	{
		$this->variable_id = $request->getParameter('variable_id');
		$variable = Doctrine_Core::getTable("VTNSVariable")->findOneByVariableId($this->variable_id);

		if ($this->variable_id) {
			$this->variable = Doctrine_Core::getTable('VTNSVariable')->find($this->variable_id);
			if ($this->variable) {
				$this->variable_form = new AdminVariableForm($this->variable);
			}
		}
        else {
            $this->variable_form = new AdminVariableForm();
        }
		if ($request->hasParameter('variable')) {
			$form = $request->getParameter('variable');
			$this->status = $this->updateVariableFromRequest($this->variable, $form);
			
			if ($this->status) {
				$this->variable->save();
				$this->redirect('variable/variableList?notify=editSuccess');
			} else
				return SfView::SUCCESS;
		} else
			return SfView::SUCCESS;
		
	}
	
	
	private function updateVariableFromRequest(&$variable, $form)
	{
		if (isset($form['variable_type']))
			$variable->variable_type = $form['variable_type'];
		if ($form['variable_name'])
			$variable->variable_name = $form['variable_name'];
		if (isset($form['variable_value']))
			$variable->variable_value = $form['variable_value'];
		if (isset($form['is_active']) && $form['is_active'] == "on")
			$variable->is_active = 1;
		else
			$variable->is_active = 0;
		
		return true;
	}
}
?>